package com.getui.demo;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * 作者： njb
 * 时间： 2019/1/16 9:53
 * 描述：
 * 来源：
 */
public class NotificationBroadcastReceiver extends BroadcastReceiver {
    public static final String TYPE = "type";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        int type = intent.getIntExtra(TYPE, -1);

        if (type != -1) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(type);
        }

        if (action.equals("notification_clicked")) {
            //处理点击事件
            String title = intent.getStringExtra("title");
            String content = intent.getStringExtra("content");
            Toast.makeText(context, "clicked " + title, Toast.LENGTH_LONG).show();
            onClick(context,title,content);
        }

        if (action.equals("notification_cancelled")) {
            //处理滑动清除和点击删除事件
            Toast.makeText(context, "cancelled", Toast.LENGTH_LONG).show();
        }
    }

    private void onClick(Context context, String title, String content) {
        Intent intent = new Intent(context,MessageInfoActivity.class);
        intent.putExtra("title",title);
        intent.putExtra("content",content);
        context.startActivity(intent);
    }

}
